import API from './index';

// 获取故事列表
export const getStories = (params) => {
  // 调整为使用 /stories/page 接口
  return API.get('/stories/page', { 
    params: {
      current: params.page || 1,
      size: params.limit || 10
    } 
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return {
        list: res.data.data.records || [],
        total: res.data.data.total || 0,
        page: res.data.data.current || 1,
        limit: res.data.data.size || 10
      };
    }
    return { list: [], total: 0, page: 1, limit: 10 };
  });
};

// 获取故事详情
export const getStoryDetail = (id) => {
  return API.get(`/stories/${id}`).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return null;
  });
};

// 获取热门故事
export const getHotStories = (params) => {
  return API.get('/stories/hot', { 
    params: {
      current: params.page || 1,
      size: params.limit || 6
    } 
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return {
        list: res.data.data.records || [],
        total: res.data.data.total || 0
      };
    }
    return { list: [], total: 0 };
  });
};

// 按分类获取故事
export const getStoriesByCategory = (category, params) => {
  return API.get(`/stories/category/${category}`, { 
    params: {
      current: params.page || 1,
      size: params.limit || 10
    } 
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return {
        list: res.data.data.records || [],
        total: res.data.data.total || 0,
        page: res.data.data.current || 1,
        limit: res.data.data.size || 10
      };
    }
    return { list: [], total: 0, page: 1, limit: 10 };
  });
};

// 获取相关故事
export const getRelatedStories = (storyId, size = 4) => {
  return API.get(`/stories/${storyId}/related`, { 
    params: { size } 
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};

// 点赞故事
export const likeStory = (storyId) => {
  return API.post(`/stories/${storyId}/like`).then(res => {
    if (res.data && res.data.code === 200) {
      return true;
    }
    return false;
  });
};

// 添加故事评论
export const addStoryComment = (storyId, data) => {
  return API.post(`/stories/${storyId}/comment`, data).then(res => {
    if (res.data && res.data.code === 200) {
      return true;
    }
    return false;
  });
};

// 获取所有故事分类
export const getAllCategories = () => {
  return API.get('/stories/categories').then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};

// 获取最新故事
export const getLatestStories = (size = 5) => {
  return API.get('/stories/latest', { 
    params: { size } 
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};

// 搜索故事
export const searchStories = (keyword, params) => {
  return API.get('/stories/search', { 
    params: {
      keyword,
      current: params.page || 1,
      size: params.limit || 10
    } 
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return {
        list: res.data.data.records || [],
        total: res.data.data.total || 0
      };
    }
    return { list: [], total: 0 };
  });
};